% Extract Feature Vectors From Data
clc;
clearvars;
DataNames='KonovalovDataPart1';
ObservDataName=['Data\HumanData_',DataNames,'.mat'];
FittingDataName=['Data\HumanData_',DataNames,'_FittingData_HF.mat'];
TotalDataFileName=['Data\HumanData_',DataNames,'_DataForAnalize_HF.mat'];

ObserveMat=load(ObservDataName);
Fitting=load(FittingDataName);
SNum=size(Fitting.BestFittedParams,1);
MethodNum=length(Fitting.Methodes);
FittingMethodsNum=size(Fitting.BestFittedParams,3)/MethodNum;
FittedW     = (shiftdim(Fitting.BestFittedParams(:,1,:),2))';
Features    = zeros(SNum,94);

%                   W   Alpha1  Alpha2  Beta1	Beta2	Lambda  P1  P2
ParamsIndex      = [1   1       0       1       0       0       0   0   %Daw3ParamV1
                    1   1       0       1       0       0       0   0   %Daw3ParamV2
                    1   1       0       1       0       1       0   0   %Daw4Param
                    1   1       1       1       1       0       0   0   %Daw5ParamV1
                    1   1       1       1       1       0       0   0   %Daw5ParamV2
                    1   1       1       1       1       1       0   0   %Daw6Param
                    1   1       1       1       1       1       1   0   %Daw7ParamV1
                    1   1       1       1       1       1       1   0   %Daw7ParamV2
                    1   1       1       1       1       1       1   1]; %Daw8Param

ParamNum=(sum(ParamsIndex,2))';
BestFittedW=zeros(SNum,FittingMethodsNum);
BestFittedAlpha=zeros(SNum,FittingMethodsNum);
BestFittedBeta=zeros(SNum,FittingMethodsNum);

for s=1:SNum
for i=1:FittingMethodsNum
    [~,I]=min(2*ParamNum+2*Fitting.BestFittedNegLogLikelihood(s,1+(i-1)*MethodNum:i*MethodNum));
    BetaIndex=sum(ParamsIndex(I,1:4));
    I=I+(i-1)*MethodNum;
    BestFittedW    (s,i) = Fitting.BestFittedParams(s,1,I);
    BestFittedAlpha(s,i) = Fitting.BestFittedParams(s,2,I);
    BestFittedBeta (s,i) = Fitting.BestFittedParams(s,BetaIndex,I);
end
end

[Stochastic,StochasticNames]=HumanDataExtractStochasticFeatures(ObserveMat,ObserveMat.Env); % 27 Features
PStay=ParallExtractDawPStay(ObserveMat);

MBAndMFIndex(:,1 )= PStay(1,1,:)+PStay(1,2,:)-PStay(2,1,:)-PStay(2,2,:);IndexNames{1} ='I_M_F^P^S^t^a^y';
MBAndMFIndex(:,2 )= PStay(1,1,:)-PStay(1,2,:)-PStay(2,1,:)+PStay(2,2,:);IndexNames{2} ='I_M_B^P^S^t^a^y';
MBAndMFIndex(:,3 )= (1-BestFittedW(:,1)).*BestFittedBeta(:,1);          IndexNames{3} ='I_M_F^M^L^E';
MBAndMFIndex(:,4 )=    BestFittedW(:,1) .*BestFittedBeta(:,1);          IndexNames{4} ='I_M_B^M^L^E';
MBAndMFIndex(:,5 )= (1-BestFittedW(:,2)).*BestFittedBeta(:,2);          IndexNames{5} ='I_M_F^M^A^P';
MBAndMFIndex(:,6 )=    BestFittedW(:,2) .*BestFittedBeta(:,2);          IndexNames{6} ='I_M_B^M^A^P';

Features(:, 1:27) = Stochastic;
Features(:,28:33) = MBAndMFIndex;
Features(:,34   ) = BestFittedW    (:,1);
Features(:,35   ) = BestFittedAlpha(:,1);
Features(:,36   ) = BestFittedBeta (:,1);
Features(:,37   ) = BestFittedW    (:,2);
Features(:,38   ) = BestFittedAlpha(:,2);
Features(:,39   ) = BestFittedBeta (:,2);
        
FittingFeaturesName={'W^M^L^E';
                     '\alpha_1^M^L^E';
                     '\beta_1^M^L^E';
                     'W^M^A^P';
                     '\alpha_1^M^A^P';
                     '\beta_1^M^A^P'};
Methodes=Fitting.Methodes;
FeaturesName = cell(1,39);
FeaturesName( 1:27)=StochasticNames(:);
FeaturesName(28:33)=IndexNames(:);
FeaturesName(34:39)=FittingFeaturesName(:);
save(TotalDataFileName,'Features','FittedW','BestFittedW','FeaturesName','Methodes','SNum')